//! 并没有严格的按照mimalloc的思路去实现，这里只是实现了一个简单的malloc底层的功能，
//! 更好的锻炼一下自己用Rust编写底层的能力。
//!
//!

mod heap;
mod link;

use core::alloc::GlobalAlloc;

#[derive(Debug, Clone, Copy)]
pub struct Mimalloc;

unsafe impl Sync for Mimalloc {}
unsafe impl Send for Mimalloc {}

#[global_allocator]
pub static ALLOCATOR: Mimalloc = Mimalloc;

unsafe impl GlobalAlloc for Mimalloc {
    unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
        let ret = unsafe { heap::malloc(layout.size()) };
        if ret.is_null() {
            panic!("ERROR:the memory wanted is too larger, allocate failed");
        }
        ret
    }

    unsafe fn dealloc(&self, ptr: *mut u8, _: core::alloc::Layout) {
        if !ptr.is_null() {
            unsafe { heap::free(ptr) };
        }
    }
}
